import sys
from PyQt6.QtWidgets import QApplication, QWidget, QVBoxLayout
from PyQt6.QtWidgets import QTableWidget, QTableWidgetItem

from PyQt6.QtWidgets import QTableWidget, QTableWidgetItem

class TableWindow(QWidget):
    def __init__(self):
        super().__init__()
        self.setWindowTitle("表格组件示例")
        self.resize(500, 300)
        
        layout = QVBoxLayout()
        
        # 创建表格组件
        table = QTableWidget(5, 3)  # 5行3列
        table.setHorizontalHeaderLabels(["姓名", "年龄", "职业"])
        
        # 填充数据
        data = [
            ["张三", "25", "工程师"],
            ["李四", "30", "设计师"],
            ["王五", "28", "教师"],
            ["赵六", "35", "医生"],
            ["钱七", "40", "律师"]
        ]
        
        for row in range(5):
            for col in range(3):
                item = QTableWidgetItem(data[row][col])
                table.setItem(row, col, item)
        
        # 单元格点击事件
        table.cellClicked.connect(self.on_cell_clicked)
        
        # 添加到布局
        layout.addWidget(table)
        self.setLayout(layout)
    
    def on_cell_clicked(self, row, column):
        item = self.sender().item(row, column)
        print(f"点击了: 行{row} 列{column} - {item.text()}")

# 使用表格窗口
app = QApplication(sys.argv)
window = TableWindow()
window.show()
sys.exit(app.exec())
